package com.example.liliang.checkboxtest;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liliang
 */
public class MainActivity extends AppCompatActivity {

    private CheckBox cb_game, cb_travel, cb_read;
    private Button btn_checkedAll, btn_uncheckedAll, btn_submit;
    private TextView resultView;
    private CheckBoxListener checkBoxListener;
    private ButtonListener buttonListener;
    private List<String> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setData();
        setListener();
    }

    private void setData() {
        list = new ArrayList<>();
    }

    private void setListener() {
        checkBoxListener = new CheckBoxListener();
        cb_game.setOnCheckedChangeListener(checkBoxListener);
        cb_travel.setOnCheckedChangeListener(checkBoxListener);
        cb_read.setOnCheckedChangeListener(checkBoxListener);
        buttonListener = new ButtonListener();
        btn_checkedAll.setOnClickListener(buttonListener);
        btn_uncheckedAll.setOnClickListener(buttonListener);
        btn_submit.setOnClickListener(buttonListener);
    }

    private void initView() {
        cb_game = findViewById(R.id.cb_game);
        cb_travel = findViewById(R.id.cb_travel);
        cb_read = findViewById(R.id.cb_read);
        btn_checkedAll = findViewById(R.id.btn_checkedAll);
        btn_uncheckedAll = findViewById(R.id.btn_uncheckedALl);
        btn_submit = findViewById(R.id.btn_submit);
        resultView = findViewById(R.id.result);

    }

    /**
     * 点击事件监听器
     */
    class ButtonListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_checkedAll:
                    cb_game.setChecked(true);
                    cb_travel.setChecked(true);
                    cb_read.setChecked(true);
                    break;
                case R.id.btn_uncheckedALl:
                    cb_game.setChecked(false);
                    cb_game.setTextColor(Color.BLACK);
                    cb_travel.setChecked(false);
                    cb_read.setChecked(false);
                    resultView.setText("");
                    break;
                case R.id.btn_submit:
                    if (cb_game.isChecked()) {
                        list.add(cb_game.getText().toString());
                    }
                    if (cb_travel.isChecked()) {
                        list.add(cb_travel.getText().toString());
                    }
                    if (cb_read.isChecked()) {
                        list.add(cb_read.getText().toString());
                    }
                    resultView.setText(list.toString());
                    list.clear();
                    break;
                default:
                    break;
            }
        }
    }

    /**
     * 复选框监听器
     */
    class CheckBoxListener implements CompoundButton.OnCheckedChangeListener {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            CheckBox checkBox = (CheckBox) buttonView;
            switch (checkBox.getId()) {
                case R.id.cb_game:
                    if (!isChecked){
                        cb_game.setTextColor(Color.BLACK);
                    } else {
                        cb_game.setTextColor(Color.RED);
                        Toast.makeText(MainActivity.this, checkBox.getText().toString(), Toast.LENGTH_SHORT).show();
                    }
                    break;
                case R.id.cb_travel:
                    Toast.makeText(MainActivity.this, checkBox.getText().toString(), Toast.LENGTH_SHORT).show();
                    break;
                case R.id.cb_read:
                    Toast.makeText(MainActivity.this, checkBox.getText().toString(), Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }
        }
    }
}
